package com.example.demo.Service;

import com.example.demo.Dao.ArticalMaper;
import com.example.demo.Model.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.session.ExpiringSession;
import org.springframework.session.SessionRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.SessionTrackingMode;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BeginView {

    @Autowired
    private ArticalMaper articalMaper;

    @Resource(name = "sessionRepository")
    private SessionRepository<ExpiringSession> sessionRepository;

    @RequestMapping("/home")
    public String home(ModelMap map, HttpServletRequest request){
        // 加入一个属性，用来在模板中读取

        HttpSession session= request.getSession();
        UserEntity userEntity= (UserEntity) session.getAttribute("user");
        if (userEntity!=null){
            map.addAttribute("user", userEntity);
            List bloglist=articalMaper.selectBlogbyuserid(userEntity.getId());
            map.addAttribute("bloglist",bloglist);

        }
        else
        {
            List bloglist=articalMaper.selectAllBlog();
            map.addAttribute("bloglist",bloglist);
        }
        return "home";
//        else
//            return "redirect:/login";


    }
    @RequestMapping("/login")
    public String test(ModelMap map){
        // 加入一个属性，用来在模板中读取
        return "login";
    }
    @RequestMapping("/register")
    public String register(ModelMap map){
        // 加入一个属性，用来在模板中读取
        return "register";
    }


    @RequestMapping("/editartical")
    public String editartical(ModelMap map, HttpServletRequest request){
        // 加入一个属性，用来在模板中读取
        HttpSession session= request.getSession();
        UserEntity userEntity= (UserEntity) session.getAttribute("user");
        if (userEntity!=null){
            map.addAttribute("user", userEntity);

            return "editartical";
        }

        return "redirect:/login";
    }
}
